package cn.itcast.bos.web.action;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.utils.MailUtils;
import cn.itcast.crm.domain.Customer;

@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class CustomerAction extends BaseAction<Customer> {
	
	

	@Action(value = "customer_sendSms")
	public String sendSms() throws IOException {
		
		//生成验证码保存到session
		String checkCode = RandomStringUtils.randomNumeric(4);
		ServletActionContext.getRequest().getSession().setAttribute("checkCode", checkCode);
		//调用发送验证码API
		System.out.println("发送验证码到手机:"+model.getTelephone() + " 验证码:"+checkCode);
		
		return NONE;
	}

	// 属性驱动
	private String checkcode;

	public void setCheckcode(String checkcode) {
		this.checkcode = checkcode;
	}
	
	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	
	@Action(value = "customer_regist", results = {
			@Result(name = "success", type = "redirect", location = "signup-success.html"),
			@Result(name = "input", type = "redirect", location = "signup.html") })
	public String regist() {
		
		//校验验证码是否正确
		String cacheCheckCode = (String) ServletActionContext.getRequest().getSession().getAttribute("checkCode");
		if(checkcode == null || !checkcode.equals(cacheCheckCode)) {
			
			System.err.print("验证码不正确cacheCheckCode"+cacheCheckCode+ " checkcode"+checkcode);
			return INPUT;
		} 
		
		//WebService注册
		WebClient.create("http://localhost:9002/crm_management/services/customerService/cust_regist")
			.post(model);
		
		//生成激活码保存到redis
		String activeCode = RandomStringUtils.randomNumeric(6);
		redisTemplate.opsForValue().set("activecode", activeCode, 24, TimeUnit.HOURS);
		
		//发送激活邮件
		MailUtils.sendMail(model.getEmail(), model.getTelephone(), activeCode);
		

		return SUCCESS;
	}
	
	@Action(value="login", results={
			@Result(name="success", type="redirect", location="/index.html"),
			@Result(name="login", type="redirect", location="/login.html")
	        })
	public String login() {
		
		
		Customer customer = WebClient
				.create("http://localhost:9002/crm_management/services/customerService/findCustomerByTelephoneAndPassword?telephone="+model.getTelephone()+"&password="+model.getPassword())
						.accept(MediaType.APPLICATION_JSON).get(Customer.class);
		if(customer == null) {
			
			return LOGIN;
		}
		
		ServletActionContext.getRequest().getSession().setAttribute("customer", customer);
		
		return SUCCESS;
	}
	
	
	

	// 属性驱动
	private String activecode;

	public void setActivecode(String activecode) {
		this.activecode = activecode;
	}

	@Action("customer_activeMail")
	public String activeMail() throws IOException {
		
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		ServletActionContext.getResponse().setContentType("text/plain; charset=utf-8");
		//判断激活码是否正确
		String cacheaActivecode = redisTemplate.opsForValue().get("activecode");
		if(cacheaActivecode==null || !cacheaActivecode.equals(activecode)) {
			
			ServletActionContext.getResponse().getWriter().write("激活码过期,请重新注册");
			return NONE;
		}
		
		//激活码正确判断用户是否已经激活了
		Customer customer = WebClient
				.create("http://localhost:9002/crm_management/services/customerService/findCustByTelephone/"+model.getTelephone())
				.accept(MediaType.APPLICATION_JSON)
				.get(Customer.class);
		
		if(customer.getType() == null || customer.getType() != 1) {
			
			WebClient
			.create("http://localhost:9002/crm_management/services/customerService/updateCustType/"+model.getTelephone())
			.put(null);
			ServletActionContext.getResponse().getWriter().write("激活成功");
			
		} else {
			
			ServletActionContext.getResponse().getWriter().write("已经激活过了");
		}
		
		return NONE;
	}

}
